/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * main.c
 * Copyright (C) Tom Keller 2008 <tpk5010@psu.edu>
 * 
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <config.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <gst/gst.h>
#include <curl/curl.h>
#include <gps.h>
#include "callbacks.h"
#include "appdata.h"
#include "interface.h"

#define GLADE_FILE PACKAGE_DATA_DIR"/nokia_eel/glade/nokia_eel.glade"
#define CONFIG_FILE PACKAGE_SYSCONF_DIR"/nokia-eel/nokia-eel.conf"

static void parse_config (AppData *app_data);

static AppData *create_data () {
	GError *error = NULL;

    AppData *result = g_new0 (AppData, 1);
    
	result->gxml = glade_xml_new( GLADE_FILE, NULL, NULL );
	g_assert(result->gxml);
    
    /* connectedToServer is set to true for debugging purposes. */
    result->connectedToServer = TRUE;
    
    result->gpsHasFix = FALSE;
    
    result->curl = curl_easy_init();
	g_assert(result->curl);
	
	result->confFile = g_key_file_new();
	result->keyFileFlags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
	if(!g_key_file_load_from_file(result->confFile, CONFIG_FILE, result->keyFileFlags, &error))
		g_error("Error loading configuration file: %s", (char*)error->message);	
	result->config = g_new0(settings, 1);
	parse_config(result);
	
	result->gpsData = g_new0(gpsdata, 1);

    return result;
}

static void parse_config (AppData *app_data) {
	AppData* data = app_data;
	GError *error = NULL;
	
	data->config->DeviceID = g_key_file_get_integer(data->confFile, "nokia-eel", "DeviceID", &error);
	if(error != NULL) 
		g_error("Loading config file: %s\nThe configuration file is malformed. Terminating application.", (char*)error->message);
	data->config->Server = g_key_file_get_string(data->confFile, "nokia-eel", "Server", &error);
	if(error != NULL)
		g_error("Loading config file: %s\nThe configuration file is malformed. Terminating application.", (char*)error->message);
	data->config->VideoSrc = g_key_file_get_string(data->confFile, "nokia-eel", "VideoSrc", &error);
	if(error != NULL)
		g_error("Loading config file: %s\nThe configuration file is malformed. Terminating application.", (char*)error->message);
	data->config->VideoDevice = g_key_file_get_string(data->confFile, "nokia-eel", "VideoDevice", &error);
	if(error != NULL)
		g_error("Loading config file: %s\nThe configuraton file is malformed. Terminating application.", (char*)error->message);
	data->config->gpsdHost = g_key_file_get_string(data->confFile, "nokia-eel", "gpsdHost", &error);
	if(error != NULL)
		g_error("Loading config file: %s\nThe configuraton file is malformed. Terminating application.", (char*)error->message);
	data->config->gpsdPort = g_key_file_get_integer(data->confFile, "nokia-eel", "gpsdPort", &error);
	if(error != NULL)
		g_error("Loading config file: %s\nThe configuraton file is malformed. Terminating application.", (char*)error->message);
	data->config->useGPS = g_key_file_get_boolean(data->confFile, "nokia-eel", "useGPS", &error);
	if(error != NULL)
		g_error("Loading config file: %s\nThe configuraton file is malformed. Terminating application.", (char*)error->message);

}

int gps_connect_with_callback(AppUIData *app_data) {
	AppUIData* uidata = app_data;
	gchar buf[6];
	if(uidata->data->config->useGPS) {
		struct gps_data_t *gpsdata;
		pthread_t gps_thread;
		g_ascii_dtostr(buf, 6, (gdouble)uidata->data->config->gpsdPort);
		gpsdata = gps_open((char*)uidata->data->config->gpsdHost, buf);
	
		gps_set_callback(gpsdata, &gps_callback, &gps_thread);
	}

	return 0;
}

static void destroy_data (AppData *data) {
    g_free (data);
}
	
int main (int argc, char *argv[])
{
 	AppData *data;
	AppUIData *main_view;

	gtk_set_locale ();
	gtk_init (&argc, &argv);
	gst_init (&argc, &argv);

	if(!g_thread_supported()) {
	    gdk_threads_init();
		g_thread_init( NULL );
	}
	
	data = create_data();
	main_view = interface_main_view_new (data);

	glade_xml_signal_autoconnect( data->gxml );
	g_signal_connect( G_OBJECT(main_view->window), "delete_event", gtk_main_quit, NULL );

	g_signal_connect( G_OBJECT(main_view->submitbutton), "clicked", G_CALLBACK(createReport), main_view );
	g_signal_connect( G_OBJECT(main_view->stopbutton), "clicked", G_CALLBACK(stopButton), main_view );
	
	gps_connect_with_callback(main_view);

	g_timeout_add(500, update_clock, main_view);

	gtk_widget_show( GTK_WIDGET(main_view->window) );
    gdk_threads_enter();
	gtk_main ();
    gdk_threads_leave();
	interface_main_view_destroy (main_view);
	curl_easy_cleanup(data->curl);
	destroy_data (data);

	exit(0);
}
